package com.bite.book.service;

import com.bite.book.enums.StatusEnum;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageResult;
import com.bite.book.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;



    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }


    public PageResult getListByPage(RequestPage requestPage) {
        //先拿到count
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(), requestPage.getPageSize());
        /*
        可以使用枚举类进行封装
        因为状态可以罗列出来
        for (BookInfo bookInfo : bookInfos) {
            if (bookInfo.getStatus() == 1) {
                bookInfo.setStateCN("可借阅");
            } else {
                bookInfo.setStateCN("不可借阅");
            }
        }*/
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<>(bookInfos, count,requestPage);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }


    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
